package com.ms.p2p.controller.admin;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.bean.Message;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.entity.redpacket.RedpacketCategory;
import com.ms.p2p.provider.RedPacketCategoryProvider;
import com.ms.p2p.utils.JsonDataGridHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chensk on 2017/11/22.
 */
@Controller("RedPacketCategoryController")
@RequestMapping({"/admin/redpacketcategory"})
public class AdminRedPacketCategoryController extends  AdminBaseController{

    @Reference
    private RedPacketCategoryProvider redPacketProvider;


    @RequestMapping({"/list"})
    public String list(Model model, PageBean page) throws IllegalAccessException,
            InvocationTargetException, IntrospectionException {

        return "/admin/redpacket/category/list";
    }


    @RequestMapping({"/queryData"})
    @ResponseBody
    public PageBean queryData(Model model, PageBean page,HttpServletRequest request) throws IllegalAccessException,
            InvocationTargetException, IntrospectionException {
        page.setList(redPacketProvider.findPager(page));
        page.setTotalCount(redPacketProvider.findCount());
        return JsonDataGridHelper.createJSONData(page);
    }

    @RequestMapping({"/add"})
    public String add(Model model, PageBean page) throws IllegalAccessException,
            InvocationTargetException, IntrospectionException {
        return "/admin/redpacket/category/input";
    }

    @RequestMapping({"/delete"})
    public String delete(@RequestParam(value = "id",required = false) String id) throws IllegalAccessException,
            InvocationTargetException, IntrospectionException {
        redPacketProvider.delete(id);
        return "/admin/redpacket/category/list";
    }


    @RequestMapping({"/save"})
    @ResponseBody
    public Message save(RedpacketCategory redpacketCategory) throws IllegalAccessException,
            InvocationTargetException, IntrospectionException {
        int result = 0;
        if(redpacketCategory.getId() == null){
            result = redPacketProvider.insert(redpacketCategory);
        } else {
            result = redPacketProvider.update(redpacketCategory);
        }

        return Message.success("操作成功");
    }


    @RequestMapping({"/modify"})
    @ResponseBody
    public Map modify(@RequestParam(value = "id",required = false) String id) throws IllegalAccessException,
            InvocationTargetException, IntrospectionException {
        HashMap retMap = new HashMap();
        if (StringUtils.isNotBlank(id)) {
               RedpacketCategory redpacketCategory  = redPacketProvider.get(id);

            if(redpacketCategory!=null){
                if(redpacketCategory.getIs_disable()==0){
                    redpacketCategory.setIs_disable(1);

                    retMap.put("status",1);
                    retMap.put("message","启用成功");

                }else{
                    redpacketCategory.setIs_disable(0);
                    retMap.put("status",1);
                    retMap.put("message","暂停成功");

                }
                redPacketProvider.update(redpacketCategory);
            }else{
                retMap.put("status",0);
                retMap.put("message","不存在");
            }

        }else{
            retMap.put("status",0);
            retMap.put("message","不存在");
        }
        return retMap;
    }


    @Override
    public boolean getBinderFlag() {
        return true;
    }
}
